﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SEO.Business;
using SEO.Entity;

/// <summary>
/// 管理权限组列表
/// </summary>
public partial class webadmin_DoAdminType : AdminPage
{
    /// <summary>
    /// ID
    /// </summary>
    protected int nID;

    /// <summary>
    /// 操作类型
    /// </summary>
    protected string strType = string.Empty;


    protected void Page_Load(object sender, EventArgs e)
    {
        strType = Common.GetQueryValue("type", 0);
        int.TryParse(Common.GetQueryValue("id", 0), out nID);

        if (!Page.IsPostBack)
        {
            if (strType == "edit")
            {
                //页面数据初始化
                InitData();
            }
            InitFeatures();
        }
    }

    /// <summary>
    /// 权限控制
    /// </summary>
    /// <returns></returns>
    public override long GetNeededFeatures()
    {
        return AdminFEATURE.UserType;
    }

    /// <summary>
    /// 数据初始化
    /// </summary>
    protected void InitData()
    {
        AdminType Info = new BisAdminInfo().GetAdminType(nID);
        if (Info != null)
        {
            txtName.Text = Info.Name;
            hfAdminFeatures.Value = Info.Features.ToString();
        }
    }

    protected void InitFeatures()
    {
        DataTable dt = new BisAdminInfo().GetAdminFeatures();
        DataTable dtNew = dt.Clone();
        foreach (DataRow dr in dt.Rows)
        {
            if (Convert.ToInt32(dr["ParentID"]) == 0)
            {
                dtNew.ImportRow(dr);
                DataRow[] drList = dt.Select("ParentID=" + dr["ID"].ToString());
                foreach (DataRow drTem in drList)
                {
                    dtNew.ImportRow(drTem);
                }
            }
        }
        rptFeatures.DataSource = dtNew;
        rptFeatures.DataBind();
    }



    /// <summary>
    /// 列表绑定处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rptFeatures_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DataTable dt = (DataTable)((Repeater)sender).DataSource;
        CheckBox chkID = e.Item.FindControl("chkID") as CheckBox;
        if (dt.Select("ParentID=" + dt.Rows[e.Item.ItemIndex]["ID"].ToString()).Length > 0)
        {
            chkID.Visible = false;
        }
        else
        {
            Label lblName = e.Item.FindControl("lblName") as Label;
            lblName.Visible = false;
            if (strType == "edit")
            {
                long nAdminFeatures=0;
                long.TryParse(hfAdminFeatures.Value,out nAdminFeatures);
                long nFeatures = 0;
                HiddenField hfFeatures = e.Item.FindControl("hfFeatures") as HiddenField;
                long.TryParse(hfFeatures.Value, out nFeatures);
                if ((nAdminFeatures & nFeatures) == nFeatures)
                {
                    chkID.Checked = true;
                }
            }
        }
    }

    /// <summary>
    /// 提交
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Int32 nFeatures=0;
        foreach (RepeaterItem item in rptFeatures.Items)
        {
            CheckBox chkID = item.FindControl("chkID") as CheckBox;
            if (chkID.Checked)
            {
                HiddenField hfFeatures = item.FindControl("hfFeatures") as HiddenField;
                nFeatures += Convert.ToInt32(hfFeatures.Value);
            }
        }
        if (nFeatures == 0)
        {
            Until.Alert("请选择权限！");
            return;
        }
        AdminType Info = new AdminType();
        Info.Name = txtName.Text.Trim();
        Info.Features = nFeatures;
        BisAdminInfo bisInfo = new BisAdminInfo();
        bool bResult = false;
        if (strType == "edit")
        {
            Info.ID = nID;
            bResult = bisInfo.UpdateAdimType(Info);
        }
        else
        {
            bResult = bisInfo.InsertAdimType(Info);
        }
        //结果处理
        if (bResult)
        {
            Until.AlertJS("提交成功！", "ResetPage();");
        }
        else
        {
            Until.Alert("提交失败！");
        }        
    }


    protected string GetStyle(object oParentID)
    {
        if (Convert.ToInt32(oParentID) > 0)
            return " style='padding-left:20px'";
        return "";
    }
}